package qc.module.demo.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.TimeRangeLengthEnum;
import qc.module.demo.dto.tspoints.TsPointsDto;
import qc.module.demo.entity.TsPoints;

import java.util.List;

/**
 * TsPointsMapper
 *
 * @author YRR
 * @create 2025-04-11 15:37
 */
@Mapper
public interface TsPointsMapper {
    TsPointsMapper MAPPER = Mappers.getMapper(TsPointsMapper.class);

    @Mapping(target = "tmtype", source = "tmtype.index")
    @Mapping(target = "tmtypestr", source = "tmtype.name")
    TsPointsDto toDto(TsPoints en);

    TsPoints toEntity(TsPointsDto dto);

    default TsPoints dtoToEntity(TsPointsDto dto) {
        //DTO转换为Entity
        TsPoints en = TsPointsMapper.MAPPER.toEntity(dto);

        en.setTmtype(getTimeRangeLengthEnum(dto.getTmtype()));

        return en;
    }

    List<TsPointsDto> toDtoList(List<TsPoints> ens);

    static TimeRangeLengthEnum getTimeRangeLengthEnum(int index) {
        if (index > 0x0) {
            for (TimeRangeLengthEnum item : TimeRangeLengthEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        //默认为空
        return null;
    }
}
